package com.peoit.honeycomb.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.peoit.honeycomb.application.UserApplication;
import com.peoit.honeycomb.domain.User;
import com.peoit.honeycomb.dto.CommonResultJsonDto;
import com.peoit.honeycomb.dto.PageCommonResultDto;
import com.peoit.honeycomb.dto.UserDto;
import com.peoit.honeycomb.dto.UserQueryDto;

/**
 * 用户审核 Created by zhouqiu on 2015/9/30.
 */
@Controller
@RequestMapping("/admin/userverify")
public class UserVerifyController extends HoneycombBaseController {
	Logger logger = Logger.getLogger(ProjectController.class.getName());

	@Autowired
	UserApplication userApplication;

	@RequestMapping("/list/{offset}")
	public ModelAndView list(UserQueryDto dto,@PathVariable int offset) {
		dto.setOffset(offset);
		List<User> list = userApplication.findAuthenWherePage(dto);
		List<UserDto> dtos = new ArrayList<>();
		for (User user : list) {
			UserDto userDto = UserDto.toDto(user);
			dtos.add(userDto);
		}

		long count = userApplication.findCountAuthen(dto);

		PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
		pageCommonResultDto.setResult(dtos);
		pageCommonResultDto.setPageSize(dto.getPageSize());
		pageCommonResultDto.setCount(count);
		pageCommonResultDto.setOffset(offset);

		return view("/jsp/member/authenUserlist", pageCommonResultDto);
	}

	/**
	 * 审核
	 * 
	 * @param userId
	 * @param passOrNot
	 * @return
	 */
	@RequestMapping("/verifyUser")
	public @ResponseBody CommonResultJsonDto verifyUser(String userId, boolean passOrNot) {
		CommonResultJsonDto result = null;
		if (passOrNot) {
			result = CommonResultJsonDto.success("审核通过！！");
		} else {
			result = CommonResultJsonDto.success("审核未通过！！");
		}
		userApplication.verifyUser(userId, passOrNot);
		return result;
	}

	/**
	 * 审核时，查询用户详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/verifyUserDetail")
	public ModelAndView verifyUserDetail(String id) {
		User user = userApplication.getUserById(id);
		return view("/jsp/member/authenUserDetail", user);
	}

}
